package group.program.Service.impl;

import group.program.Config.DatabaseConfig;
import group.program.Config.PromptConfig;
import group.program.Service.DatabaseService;
import group.program.context.BaseContext;
import group.program.mapper.DatabaseMapper;
import group.program.pojo.Dbstructure;
import group.program.properties.DataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.underlying.common.config.DataSourceConfiguration;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DatabaseServiceImpl implements DatabaseService {

    @Autowired
    private DatabaseMapper databaseMapper;

    @Autowired
    private PromptConfig promptConfig;

    DatabaseConfig databaseConfig = new DatabaseConfig();

    @Override
    public void addDatabase(Dbstructure dbstructure) {
        DataSourceProperties properties = new DataSourceProperties();
        //属性拷贝
        BeanUtils.copyProperties(dbstructure,properties);
        log.info("Dbstructure: {}", dbstructure.toString()); // 确保 dbstructure 的值是正确的
        log.info("Properties: {}", properties.toString()); // 确保 properties 的值是正确的
        //添加连接池中的数据库连接（真实连接）
        databaseConfig.addDataSource(properties);
        //存储到记录中（物理存储）
        Integer uid = BaseContext.getCurrentId();
        dbstructure.setUid(uid);
        databaseMapper.addDatabase(dbstructure);
//        //prompt工程更新
//        String newPrompt = databaseMapper.selectDbStructure(uid);
//        promptConfig.setBeginPrompt(newPrompt + promptConfig.getBeginPrompt());
//        log.info("最新prompt：",promptConfig.getBeginPrompt());

//        databaseConfig.addName(dbstructure.getName());
//        databaseConfig.addUrl(dbstructure.getUrl());
//        databaseConfig.addUsername(dbstructure.getUsername());
//        databaseConfig.addPassword(dbstructure.getPassword());
    }

    @Override
    public void removeDatabase(Integer did) {
        Dbstructure dbstructure = databaseMapper.selectOne(did,BaseContext.getCurrentId());
//        databaseConfig.removeName(dbstructure.getName());
//        databaseConfig.removeUrl(dbstructure.getUrl());
//        databaseConfig.removeUsername(dbstructure.getUsername());
//        databaseConfig.removePassword(dbstructure.getPassword());
        //删除库里记录（物理存储）
        Integer uid = BaseContext.getCurrentId();
        dbstructure.setUid(uid);
        databaseMapper.rmDatabase(did,uid);
//        //prompt工程更新
//        String newPrompt = databaseMapper.selectDbStructure(dbstructure.getDid(),uid);
//        String updatedBeginPrompt = promptConfig.getBeginPrompt().replace(newPrompt, "");
//        promptConfig.setBeginPrompt(updatedBeginPrompt);
        log.info("最新prompt：",promptConfig.getBeginPrompt());
    }

    @Override
    public List<Dbstructure> selectAll() {
        Integer uid = BaseContext.getCurrentId();
        List<Dbstructure> ls = databaseMapper.selectAll(uid);
        return ls;
    }

    @Override
    public List<Dbstructure> selectByName(String name) {
        Integer uid = BaseContext.getCurrentId();
        List<Dbstructure> ls = databaseMapper.selectByName(name,uid);
        return ls;
    }
}
